package com.fr.base.core.html;

import com.fr.base.CodeUtils;
import com.fr.base.core.BaseCoreUtils;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Tag extends Html
{
  private String tagName;
  private List classes;
  private Map styles;
  private Map attributes;
  private List subHtmlList;
  private List siblingHtmlList;

  public Tag()
  {
    this(null);
  }

  public Tag(String paramString)
  {
    this.tagName = paramString;
  }

  public Tag cls(String paramString)
  {
    if (this.classes == null)
      this.classes = new ArrayList();
    this.classes.add(paramString);
    return this;
  }

  public Tag css(String paramString1, String paramString2)
  {
    if (this.styles == null)
      this.styles = new HashMap();
    this.styles.put(paramString1, paramString2);
    return this;
  }

  public Tag css(Map paramMap)
  {
    if (this.styles == null)
      this.styles = new HashMap();
    this.styles.putAll(paramMap);
    return this;
  }

  public Tag attr(String paramString1, String paramString2)
  {
    if (this.attributes == null)
      this.attributes = new HashMap();
    this.attributes.put(paramString1, paramString2);
    return this;
  }

  public Tag sub(Html paramHtml)
  {
    if (this.subHtmlList == null)
      this.subHtmlList = new ArrayList();
    this.subHtmlList.add(paramHtml);
    return this;
  }

  public Tag sub(String paramString)
  {
    return sub(new TextHtml(paramString));
  }

  public Tag sibling(Html paramHtml)
  {
    if (this.siblingHtmlList == null)
      this.siblingHtmlList = new ArrayList();
    this.siblingHtmlList.add(paramHtml);
    return this;
  }

  public void writeHtml(PrintWriter paramPrintWriter)
  {
    if (this.tagName == null)
    {
      i = 0;
      j = (this.subHtmlList == null) ? 0 : this.subHtmlList.size();
      while (i < j)
      {
        ((Html)this.subHtmlList.get(i)).writeHtml(paramPrintWriter);
        ++i;
      }
      return;
    }
    paramPrintWriter.write("<" + this.tagName);
    paramPrintWriter.write(classBuffer().toString());
    paramPrintWriter.write(styleBuffer().toString());
    paramPrintWriter.write(attributeBuffer().toString());
    if ((this.tagName.equalsIgnoreCase("COL")) && (((this.subHtmlList == null) || (this.subHtmlList.size() == 0))))
    {
      paramPrintWriter.write(" />");
      return;
    }
    paramPrintWriter.write(">");
    int i = 0;
    int j = (this.subHtmlList == null) ? 0 : this.subHtmlList.size();
    while (i < j)
    {
      ((Html)this.subHtmlList.get(i)).writeHtml(paramPrintWriter);
      ++i;
    }
    paramPrintWriter.write("</" + this.tagName + ">");
    if (this.siblingHtmlList != null)
    {
      i = 0;
      j = this.siblingHtmlList.size();
      while (i < j)
      {
        ((Html)this.siblingHtmlList.get(i)).writeHtml(paramPrintWriter);
        ++i;
      }
    }
  }

  private StringBuffer classBuffer()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if ((this.classes != null) && (this.classes.size() > 0))
    {
      localStringBuffer.append(" class=\"");
      int i = this.classes.size();
      for (int j = 0; j < i - 1; ++j)
      {
        localStringBuffer.append(this.classes.get(j));
        localStringBuffer.append(' ');
      }
      localStringBuffer.append(this.classes.get(i - 1));
      localStringBuffer.append('"');
    }
    return localStringBuffer;
  }

  private StringBuffer styleBuffer()
  {
    StringBuffer localStringBuffer1 = new StringBuffer();
    if (this.styles != null)
    {
      StringBuffer localStringBuffer2 = new StringBuffer();
      Iterator localIterator = this.styles.entrySet().iterator();
      while (localIterator.hasNext())
      {
        Map.Entry localEntry = (Map.Entry)localIterator.next();
        localStringBuffer2.append(localEntry.getKey()).append(":").append(localEntry.getValue()).append(";");
      }
      if (localStringBuffer2.length() > 0)
        localStringBuffer1.append(" style=\"").append(localStringBuffer2).append("\"");
    }
    return localStringBuffer1;
  }

  private StringBuffer attributeBuffer()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (this.attributes != null)
    {
      Iterator localIterator = this.attributes.entrySet().iterator();
      while (localIterator.hasNext())
      {
        Map.Entry localEntry = (Map.Entry)localIterator.next();
        localStringBuffer.append(" ").append(localEntry.getKey()).append("=\"").append(CodeUtils.attributeHtmlEncode(localEntry.getValue().toString())).append("\"");
      }
    }
    return localStringBuffer;
  }

  public String toString()
  {
    return toHtml();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Tag localTag = (Tag)clone();
    localTag.attributes = BaseCoreUtils.cloneMap(this.attributes);
    localTag.styles = BaseCoreUtils.cloneMap(this.styles);
    return localTag;
  }
}